package com.qf.electronic.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.electronic.dto.DefectTaskReceptionDto;
import com.qf.electronic.dto.DefectTaskReceptionStateDto;
import com.qf.electronic.pojo.DefectTask;
import com.qf.electronic.pojo.DefectTaskReception;
import com.qf.electronic.mapper.DefectTaskReceptionMapper;
import com.qf.electronic.pojo.Inspection;
import com.qf.electronic.pojo.InspectionReception;
import com.qf.electronic.service.IDefectTaskReceptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.electronic.service.IDefectTaskService;
import com.qf.electronic.vo.DefectTaskReceptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wu
 * @since 2023-05-04
 */
@Service
public class DefectTaskReceptionServiceImpl extends ServiceImpl<DefectTaskReceptionMapper, DefectTaskReception> implements IDefectTaskReceptionService {

    @Autowired
    private IDefectTaskService defectTaskService;

    @Transactional(readOnly = true, isolation = Isolation.READ_COMMITTED)
    @Override
    public Page<DefectTaskReceptionVo> getDefectTaskReceptions(DefectTaskReceptionDto receptionDto) {
        Page<DefectTaskReception> page = new Page<>(receptionDto.getCurrentPage(), receptionDto.getPageSize());
        return this.baseMapper.getDefectTaskReceptions(page, receptionDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeDefectTaskReceptionState(DefectTaskReceptionStateDto stateDto) {
        Integer state = stateDto.getState();
        UpdateWrapper<DefectTaskReception> wrapper = new UpdateWrapper<>();
        wrapper.set("state", state);
        wrapper.eq("id", stateDto.getId());
        if (update(wrapper)) {//状态修改成功 且状态为审核通过
            if(state == 2){
                DefectTaskReception reception = getById(stateDto.getId());
                UpdateWrapper<DefectTask> taskWrapper = new UpdateWrapper<>();
                taskWrapper.set("state", 3);
                taskWrapper.eq("id", reception.getDefectTaskId());
                return defectTaskService.update(taskWrapper);
            }
           return true;
        }
        return false;
    }
}
